package util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;

import alexico.Simbolo;

public class FileUtils {
	public static String loadFile(String path){
		String codigo = new String();
		BufferedReader input = null;
		try {
			input = new BufferedReader( new FileReader(path));
			String linea = null;
			while (( linea = input.readLine()) != null){
				codigo += linea;
				codigo += System.getProperty("line.separator");
			}
		}
		catch (FileNotFoundException ex) {
			ex.printStackTrace();
		}
		catch (IOException ex){
			ex.printStackTrace();
		}
		finally {
			try {
				if (input!= null) {
					input.close();
				}
			}
			catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return codigo;
	}
	public static void writeLine (File file, String linea){		
		try {
			BufferedWriter input = new BufferedWriter( new FileWriter(file,true));
			input.write(linea+System.getProperty("line.separator"));
			input.close();
		} catch (IOException ex){
			ex.printStackTrace();
		}
	}
	
	public static void pasarArchivo (HashMap<String, Simbolo> tabla, File path){
		try {
			BufferedWriter input = new BufferedWriter( new FileWriter(path,true));
			for (Simbolo s:tabla.values()){
				input.write(s.toString()+System.getProperty("line.separator"));
			}
			input.close();
		} catch (IOException ex){
			ex.printStackTrace();
		}
	}
}